package org.noote.LineDetection;

import java.io.IOException;

import org.noote.libs.graphicbuffer.GraphicView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageView extends GraphicView {
	
	public ImageView(Context context) {
        super(context);
    }
	
	@Override
	protected void onSizeChanged(int width, int height, int width_old, int height_old)
	{
		super.onSizeChanged(width, height, width_old, height_old);

		createBuffer(width, height);
		
	    drawImage();
	}

	private void drawImage()
	{
		Bitmap loadedBitmap = null;
		try {
			loadedBitmap = BitmapFactory.decodeStream(getContext().getAssets().open("IMAG0082.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	    
		if(loadedBitmap != null)
		{
		    Bitmap resizedBitmap = Bitmap.createScaledBitmap(loadedBitmap, _buffer.getWidth(), _buffer.getHeight(), true);
		    
		    // Transfert data from resizedBitmap => _buffer => _bitmap
		    resizedBitmap.getPixels(_buffer.getBuffer(), 0, _buffer.getWidth(), 0, 0, _buffer.getWidth(), _buffer.getHeight());
		}
		
		refresh();
	}
	
	public void copy_into(LineDetection detect)
	{
		if(_buffer != null)
		{
			detect.copy_from(_buffer.getBuffer(), _buffer.getWidth(), _buffer.getHeight());
		}
	}
}
